<template>
  <div class="user-page" v-if="user">
    <div class="user-page-head">
      <div class="top">
        <van-image round fit="cover" :src="user.avatar"></van-image>
        <div class="name">
          <p>{{ user.account }}</p>
          <p><van-icon name="edit"></van-icon></p>
        </div>
      </div>
      <van-row>
        <van-col :span="6" v-for="item in list" :key="item.key">
          <p>{{ user[item.key] }}</p>
          <p>{{ item.name }}</p>
        </van-col>
      </van-row>
    </div>
    <div class="user-page-other">
      <div class="head">
        <h3>药品订单</h3>
        <router-link to="/other"
          >全部订单 <van-icon name="arrow"></van-icon>
        </router-link>
      </div>
      <van-row>
        <van-col span="6" v-for="mode in drugsList" :key="mode.icon">
          <van-badge :content="user.orderInfo[mode.content]">
            <cp-icon :name="`user-${mode.icon}`"></cp-icon>
          </van-badge>
          <p>{{ mode.name }}</p>
        </van-col>
      </van-row>
    </div>
    <div class="user-page-group">
      <h3>快捷工具</h3>
      <van-cell
        v-for="(item, i) in tools"
        :key="item.label"
        :title="item.label"
        :to="item.path"
        is-link
        :border="false"
      >
        <template #icon><cp-icon :name="`user-tool-0${i + 1}`" /></template>
      </van-cell>
    </div>
    <a class="logout" href="javascript:;" @click="out">退出登录</a>
  </div>
</template>

<script setup lang="ts">
import { getUserInfo } from '@/services/user'
import type { UserInfo } from '@/types/user'
import { onMounted, ref } from 'vue'
import { showConfirmDialog } from 'vant'
import { useUserStore } from '@/stores'
import router from '@/router'
const user = ref<UserInfo>()
const store = useUserStore()

const out = async () => {
  await showConfirmDialog({
    title: '提示',
    message: '确认退出?'
  })
  store.deleUser()
  router.replace('/login')
}

type List = {
  key: 'collectionNumber' | 'likeNumber' | 'score' | 'couponNumber'
  name: string
}
const list: List[] = [
  { key: 'collectionNumber', name: '收藏' },
  { key: 'likeNumber', name: '关注' },
  { key: 'score', name: '积分' },
  { key: 'couponNumber', name: '优惠券' }
]
type Drugs = {
  content: 'paidNumber' | 'receivedNumber' | 'shippedNumber' | 'finishedNumber'
  name: string
  icon: string
}

const drugsList: Drugs[] = [
  { name: '待付款', content: 'paidNumber', icon: 'paid' },
  { name: '待发货', content: 'receivedNumber', icon: 'shipped' },
  { name: '待收货', content: 'shippedNumber', icon: 'received' },
  { name: '已完成', content: 'finishedNumber', icon: 'finished' }
]

const tools = [
  { label: '我的问诊', path: '/user/consult' },
  { label: '我的处方', path: '/' },
  { label: '家庭档案', path: '/user/patient' },
  { label: '地址管理', path: '/user/address' },
  { label: '我的评价', path: '/' },
  { label: '官方客服', path: '/' },
  { label: '设置', path: '/' }
]

onMounted(async () => {
  const res = await getUserInfo()
  user.value = res.data
})
</script>

<style lang="scss" scoped>
.user-page {
  background-color: var(--cp-bg);
  min-height: calc(100vh - 50px);
  padding: 0 15px 65px;
  &-head {
    height: 200px;
    background: linear-gradient(
      180deg,
      rgba(44, 181, 165, 0.46),
      rgba(44, 181, 165, 0)
    );
    margin: 0 -15px;
    padding: 0 15px;
    .top {
      display: flex;
      padding-top: 50px;
      align-items: center;
      .van-image {
        width: 70px;
        height: 70px;
      }
      .name {
        padding-left: 10px;
        p {
          &:first-child {
            font-size: 18px;
            font-weight: 500;
          }
          &:last-child {
            margin-top: 10px;
            color: var(--cp-primary);
            font-size: 16px;
          }
        }
      }
    }
    .van-row {
      margin: 0 -15px;
      padding-top: 15px;
      p {
        text-align: center;
        &:first-child {
          font-size: 18px;
          font-weight: 500;
        }
        &:last-child {
          color: var(--cp-dark);
          font-size: 12px;
          padding-top: 4px;
        }
      }
    }
  }
  &-other {
    background-color: #fff;
    border-radius: 8px;
    margin-bottom: 15px;
    padding-bottom: 15px;
    .head {
      display: flex;
      justify-content: space-between;
      line-height: 50px;
      padding: 0 15px;
      a {
        color: var(--cp-tip);
      }
    }
    .van-col {
      text-align: center;
      .cp-icon {
        font-size: 28px;
      }
      p {
        font-size: 12px;
        padding-top: 4px;
      }
    }
  }
  &-group {
    background-color: #fff;
    border-radius: 8px;
    overflow: hidden;
    h3 {
      padding-left: 16px;
      line-height: 44px;
    }
    .van-cell {
      align-items: center;
    }
    .cp-icon {
      font-size: 17px;
      margin-right: 10px;
    }
  }
  .logout {
    display: block;
    margin: 20px auto;
    width: 100px;
    text-align: center;
    color: var(--cp-price);
  }
}
</style>
